<?php
/**
 * 话题----热门话题旁边的----分类
 */
require_once dirname(dirname(__DIR__)) . "/common.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\UserMaterial;

//参数接收
$type_id = $_REQUEST['type_id'];
if (empty($type_id)) {
    Response::output(-1, "缺少参数");
}

//分页参数
$page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
$page = $page > 0 ? $page : 1;
$pageSize = 20;

//实例化数据库
$db = SysResource::getDB(false);
$UserMaterial = new UserMaterial($db);


//1.先去查询这些分类下的话题
$da1 = $UserMaterial->fenlie_guanlian_huati_type_sub($type_id, $page, $pageSize);
if($_GET['debug']){
    echo $db->last_query();
}
if (empty($da1)) {
    Response::output(0, "查询成功1", []);
}
//2.再根据这些分类下的话题去查询用户上传的属于这个话题的
foreach ($da1 as $k => &$v) {
    $user_material_select = $UserMaterial->fenlie_guanlian_huati_user_material($v['id'], $page, $pageSize);
    if($_GET['debug']){
        echo $db->last_query();
    }
    if (empty($user_material_select)) {
        unset($da1[$k]);
    } else {
        $v['topic_video_data'] = $user_material_select;
    }
}
if($_GET['debug']){
    exit;
}
if (empty($da1)) {
    Response::output(0, "查询成功2", []);
} else {
    $da2 = [];
    foreach ($da1 as $kk => $vv) {
        $da2[] = $vv;
    }
    Response::output(0, "查询成功", $da2);
}

